#ifndef BSP_CAN_H
#define BSP_CAN_H
#include "hc32_ll.h"
#include "ev_hc32f448_lqfp80_bsp.h"
#include "CanOpen_Porting.h"
#define MCAU_UNIT1                      (1U)
#define MCAU_UNIT2                      (2U)
#define MCAU_UNIT_SEL                   MCAU_UNIT1

#if (MCAU_UNIT_SEL == MCAU_UNIT1)
#define MCAN_UNIT                       (CM_MCAN1)
#define MCAN_PERIPH_CLK                 (FCG1_PERIPH_MCAN1)
#define MCAN_CLK_UNIT                   (CLK_MCAN1)
#define MCAN_CLK_SRC                    (CLK_MCANCLK_SYSCLK_DIV5)
/* Pin */
#define MCAN_TX_PORT                    (GPIO_PORT_C)
#define MCAN_TX_PIN                     (GPIO_PIN_12)
#define MCAN_TX_PIN_FUNC                (GPIO_FUNC_56)
#define MCAN_RX_PORT                    (GPIO_PORT_D)
#define MCAN_RX_PIN                     (GPIO_PIN_00)
#define MCAN_RX_PIN_FUNC                (GPIO_FUNC_57)
/* IRQ */
#define MCAN_INT0_PRIO                  (DDL_IRQ_PRIO_03)
#define MCAN_INT0_SRC                   (INT_SRC_MCAN1_INT0)
#define MCAN_INT0_IRQn                  (MCAN1_INT0_IRQn)
#define MCAN_INT_LINE0_HANDLER          MCAN1_INT0_Handler
#define MCAN_INT1_PRIO                  (DDL_IRQ_PRIO_03)
#define MCAN_INT1_SRC                   (INT_SRC_MCAN1_INT1)
#define MCAN_INT1_IRQn                  (MCAN1_INT1_IRQn)
#define MCAN_INT_LINE1_HANDLER          MCAN1_INT1_Handler

#elif (MCAU_UNIT_SEL == MCAU_UNIT2)
#define MCAN_UNIT                       (CM_MCAN2)
#define MCAN_PERIPH_CLK                 (FCG1_PERIPH_MCAN2)
#define MCAN_CLK_UNIT                   (CLK_MCAN2)
#define MCAN_CLK_SRC                    (CLK_MCANCLK_SYSCLK_DIV5)
/* Pin */
#define MCAN_TX_PORT                    (GPIO_PORT_H)
#define MCAN_TX_PIN                     (GPIO_PIN_02)
#define MCAN_TX_PIN_FUNC                (GPIO_FUNC_56)
#define MCAN_RX_PORT                    (GPIO_PORT_E)
#define MCAN_RX_PIN                     (GPIO_PIN_04)
#define MCAN_RX_PIN_FUNC                (GPIO_FUNC_57)
/* IRQ */
#define MCAN_INT0_PRIO                  (DDL_IRQ_PRIO_03)
#define MCAN_INT0_SRC                   (INT_SRC_MCAN2_INT0)
#define MCAN_INT0_IRQn                  (MCAN2_INT0_IRQn)
#define MCAN_INT_LINE0_HANDLER          MCAN2_INT0_Handler
#define MCAN_INT1_PRIO                  (DDL_IRQ_PRIO_03)
#define MCAN_INT1_SRC                   (INT_SRC_MCAN2_INT1)
#define MCAN_INT1_IRQn                  (MCAN2_INT1_IRQn)
#define MCAN_INT_LINE1_HANDLER          MCAN2_INT1_Handler

#else
#error "Not supported selection."
#endif

/* Interrupt */
#define MCAN_RX_INT_SEL                 (MCAN_INT_RX_FIFO0_NEW_MSG | \
                                         MCAN_INT_RX_FIFO0_WATERMARK | \
                                         MCAN_INT_RX_FIFO0_FULL | \
                                         MCAN_INT_RX_FIFO0_MSG_LOST | \
                                         MCAN_INT_RX_FIFO1_NEW_MSG | \
                                         MCAN_INT_RX_FIFO1_WATERMARK | \
                                         MCAN_INT_RX_FIFO1_FULL | \
                                         MCAN_INT_RX_FIFO1_MSG_LOST | \
                                         MCAN_INT_RX_BUF_NEW_MSG)
#define MCAN_TX_INT_SEL                 (MCAN_INT_TX_CPLT | \
                                         MCAN_INT_TX_EVT_FIFO_NEW_DATA | \
                                         MCAN_INT_BUS_OFF)

#define MCAN_INT0_SEL                   MCAN_RX_INT_SEL
#define MCAN_INT1_SEL                   MCAN_TX_INT_SEL

/* Message RAM */
/* Each standard filter element size is 4 bytes */
#define MCAN_STD_FILTER_NUM             (4U)
/* Each extended filter element size is 8 bytes */
#define MCAN_EXT_FILTER_NUM             (4U)
/* Each Rx FIFO0 element size is 8+8 bytes */
#define MCAN_RX_FIFO0_NUM               (10U)
#define MCAN_RX_FIFO0_WATERMARK         (8U)
#define MCAN_RX_FIFO0_DATA_FIELD_SIZE   MCAN_DATA_SIZE_8BYTE
/* Each Rx FIFO1 element size is 8+8 bytes */
#define MCAN_RX_FIFO1_NUM               (10U)
#define MCAN_RX_FIFO1_WATERMARK         (7U)
#define MCAN_RX_FIFO1_DATA_FIELD_SIZE   MCAN_DATA_SIZE_8BYTE
/* Each Rx buffer element size is 8+8 bytes */
#define MCAN_RX_BUF_NUM                 (6U)
#define MCAN_RX_BUF_DATA_FIELD_SIZE     MCAN_DATA_SIZE_8BYTE
/* Each Tx buffer element size is 8+8 bytes */
#define MCAN_TX_BUF_NUM                 (12U)
#define MCAN_TX_FIFO_NUM                (6U)
#define MCAN_TX_BUF_DATA_FIELD_SIZE     MCAN_DATA_SIZE_8BYTE
#define MCAN_TX_NOTIFICATION_BUF        ((1UL << (MCAN_TX_BUF_NUM + MCAN_TX_FIFO_NUM)) - 1U)
/* Each extended filter element size is 8 bytes */
#define MCAN_TX_EVT_NUM                 (12U)

/* Filter */
#define MCAN_CFG_IGNOR                  (0U)
/* Accept standard frames with ID from 0x110 to 0x11F and store to Rx FIFO0 */
#define MCAN_STD_FILTER0                {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 0U, .u32FilterType = MCAN_FILTER_RANGE, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_FIFO0, .u32FilterId1 = 0x110UL, \
                                         .u32FilterId2 = 0x11FUL,}
/* Accept standard frames with ID 0x130~0x132 and store to dedicated Rx buffer 0~2 */
#define MCAN_STD_FILTER1                {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 1U, .u32FilterType = MCAN_CFG_IGNOR, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x130UL, \
                                         .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF0}
#define MCAN_STD_FILTER2                {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 2U, .u32FilterType = MCAN_CFG_IGNOR, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x131UL, \
                                         .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF1}
#define MCAN_STD_FILTER3                {.u32IdType = MCAN_STD_ID, .u32FilterIndex = 3U, .u32FilterType = MCAN_CFG_IGNOR, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x132UL, \
                                         .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF2}

/* Accept extended frames with ID from 0x12345110 to 0x1234511F and store to Rx FIFO1 */
#define MCAN_EXT_FILTER0                {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 0U, .u32FilterType = MCAN_FILTER_MASK, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_FIFO1, .u32FilterId1 = 0x12345110UL, \
                                         .u32FilterId2 = 0x1FFFFFF0UL,}
/* Accept extended frames with ID 0x12345130~0x12345132 and store to dedicated Rx buffer 3~5 */
#define MCAN_EXT_FILTER1                {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 1U, .u32FilterType = MCAN_CFG_IGNOR, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x12345130UL, \
                                         .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF3}
#define MCAN_EXT_FILTER2                {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 2U, .u32FilterType = MCAN_CFG_IGNOR, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x12345131UL, \
                                         .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF4}
#define MCAN_EXT_FILTER3                {.u32IdType = MCAN_EXT_ID, .u32FilterIndex = 3U, .u32FilterType = MCAN_CFG_IGNOR, \
                                         .u32FilterConfig = MCAN_FILTER_TO_RX_BUF, .u32FilterId1 = 0x12345132UL, \
                                         .u32FilterId2 = MCAN_CFG_IGNOR, .u32RxBufferIndex = MCAN_RX_BUF5}

///////////////////////////

int32_t bsp_can_init(void);

#endif


